package com.aoyama.juc._06_block;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author ：qs
 * @date ：Created in 2022/7/18
 * @description：
 * @modified By：
 */
public class ProducerDemo2 {
    public static void main(String[] args) {
        MyResource myResource = new MyResource(new ArrayBlockingQueue<>(10));
        new Thread(() ->{
            System.out.println("----------生产者线程启动-----------");
            try {
                myResource.produce();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"Producer").start();


        new Thread(() ->{
            System.out.println("----------消费者线程启动-----------");
            try {
                myResource.consume();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"Consumer").start();


        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        myResource.stop();
        System.out.println("********5秒之后,main叫停生产,生产结束*********");
    }
}

class MyResource{
    private volatile Boolean FLAG = Boolean.TRUE;
    private AtomicInteger atomicInteger = new AtomicInteger();
    private BlockingQueue<String> blockingQueue = null;

    public MyResource(BlockingQueue<String> blockingQueue){
        this.blockingQueue = blockingQueue;
        //打印日志一般需要看类信息
        System.out.println(blockingQueue.getClass().getName());
    }

    public void produce() throws InterruptedException {
        String data = null;
        Boolean returnValue;
        while(FLAG){
            data = atomicInteger.incrementAndGet() + "";
            returnValue = blockingQueue.offer(data, 2L, TimeUnit.SECONDS);
            if (returnValue){
                System.out.println(Thread.currentThread().getName() + "\t\t 插入队列成功 \t" + data);
            }else {
                System.out.println(Thread.currentThread().getName() + "\t 插入超时 \t" + data);
            }
            try {
                TimeUnit.MILLISECONDS.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println(Thread.currentThread().getName() + "\t 大老板叫停,生产者停止生产");
    }

    public void consume() throws InterruptedException {
        String data = null;
        while(FLAG){
//            Thread.sleep(500);
            data = blockingQueue.poll(2L,TimeUnit.SECONDS);
            if (data == null || data.equalsIgnoreCase("")){
                FLAG = false;
                System.out.println(Thread.currentThread().getName() + "\t 消费超时,消费者退出" );
                return;
            }
            System.out.println(Thread.currentThread().getName() + "\t 消费队列成功 \t" + data);
        }
    }

    public void stop(){
        this.FLAG = false;
    }
}